package qplayas

import com.gargoylesoftware.htmlunit.BrowserVersion;

class PlayaWeatherJob {

	static triggers = {
		simple name: 'playaWeatherJob', startDelay: 600000, repeatInterval: 604800000l
	}
	
    def execute() {
		def beachName = 'Gandia'
		def provincia = 'Valencia'
		
         def builder = 
            new qplayas.crawling.builder.HtmlUnitBuilder().
                client(BrowserVersion.FIREFOX_3_6) {            
                    page(url:'http://www.aemet.es/es/eltiempo/prediccion/playas') {
                      form('frmLoc','form_submit'){
                          fields{
                              str = beachName
                          }
                          page{
							/* Nos aseguramos que la playa es de la provincia que queremos */
							  def fr = elementsByXPath("//div[@class='resultados_busqueda']/ul/li").find{it.getFirstByXPath('p').textContent.contains(provincia)}
                            /* Si existe una playa con esas caracteristicas entonces recopilamos sus datos */
                              if (fr){
                                  def firstResult = fr.getFirstByXPath('a').hrefAttribute
								  def firstResultQualifiedUrl = currentPage.getFullyQualifiedUrl(firstResult).toString()
								  page(url:firstResultQualifiedUrl){
									  def tiempoPrimerDiaManana = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[1]/td[1]/img/@title")[0].textContent
									  def tiempoPrimerDiaTarde = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[1]/td[2]/img/@title")[0].textContent                                  
									  def tiempoSegundoDiaManana = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[1]/td[3]/img/@title")[0].textContent                                  
									  def tiempoSegundoDiaTarde = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[1]/td[4]/img/@title")[0].textContent                                  
									  def vientoPrimerDiaManana = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[2]/td[1]")[0].textContent
									  def vientoPrimerDiaTarde = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[2]/td[2]")[0].textContent                                  
									  def vientoSegundoDiaManana = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[2]/td[3]")[0].textContent                                  
									  def vientoSegundoDiaTarde = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[2]/td[4]")[0].textContent   
									  def oleajePrimerDiaManana = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[3]/td[1]")[0].textContent
									  def oleajePrimerDiaTarde = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[3]/td[2]")[0].textContent                                  
									  def oleajeSegundoDiaManana = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[3]/td[3]")[0].textContent                                  
									  def oleajeSegundoDiaTarde = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[3]/td[4]")[0].textContent                                     
                                 
									  println "ol:"+oleajeSegundoDiaTarde
                                 
									  def tmpMaximaPrimerDia = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[4]/td[1]/div")[0].textContent                                  
									  def tmpMaximaSegundoDia = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[4]/td[2]/div")[0].textContent
                                                               	
									  println "tmp:"+tmpMaximaSegundoDia
                                                                       
									  def sensacionTermicaPrimerDia = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[5]/td[1]")[0].textContent                                  
									  def sensacionTermicaMaximaSegundoDia = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[5]/td[2]")[0].textContent
                                  
									  println "sen:"+sensacionTermicaMaximaSegundoDia
                                  
									  def tmpAguaPrimerDia = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[6]/td[1]")[0].textContent                                  
									  def tmpAguaSegundoDia = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[6]/td[2]")[0].textContent
                                  
									  println "agua:"+tmpAguaSegundoDia
                                  
									  def uvPrimerDia = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[7]/td[1]/span")[0].textContent                                  
									  def uvSegundoDia = elementsByXPath("//table[@class='tabla_datos']/tbody/tr[7]/td[2]/span")[0].textContent
                                  
									  println "uv:"+uvSegundoDia
								  }
                              }
                          }
                      }
                    }
                }
    }
}
